/**
 * @(#) BankAccountProxy.as
 *
 * @date		2009. 03. 19.
 * @version		1.0
 * @author		Park, Juneho
 * @description	 경조사 Proxy - Proxy
 * 				경조사 계좌관리 한다.
 */
package com.skt.common_hr.welfare.model
{
	import com.skt.common_hr.common.puremvc.CommonProxy;
	import com.skt.common_hr.common.puremvc.DataReceiver;
	import com.skt.common_hr.welfare.WelfareFacade;
	import com.skt.common_hr.welfare.model.vo.BankAccountVO;
	import com.skt.common_hr.welfare.model.vo.CodeVO;
	
	import mx.collections.ArrayCollection;
		
	public class BankAccountProxy extends CommonProxy
	{
		public static const NAME:String = "BankAccountProxy";
		
		public var defaultBank:BankAccountVO = null;
		public var additionBank:BankAccountVO = null;
		public var tmpBank:BankAccountVO = null;
		
		public var bankCodeList:ArrayCollection;
		public var msgStr:String = "";
		
		
		/**
		 * 생성자
		 * 
		 * @param
		 * @return void
		 */
		public function BankAccountProxy(data:Object=null)
		{
			super(NAME, data);
		}
		
		
		/**
		 * data load를 수행
		 * 		- DataLoader 를 생성하고 Import Parameter 객체를 생성, RFC를 호출
		 * 기타계좌 콤보박스 조회 
		 * @param 
		 * @return void
		 */
		public function load(pernr:String):void
		{
			var param:Object = new Object();
			loadMultiData("Z_HR_0051", param, bankCodeResult);
			
			param = new Object;
			param["I_PERNR"] = pernr;
			loadMultiData("Z_HR_0042", param, accountResult);
		}
		
		
		public function save(pernr:String, vo:BankAccountVO):void
		{
			var param:Object = new Object();
			param["I_PERNR"] = pernr;
			
			var tables:Object = new Object();
			var table:Array = new Array();
			table.push(vo);
			tables["BTAB"] = table;
			
			tmpBank = vo;
			saveMultiData("Z_HR_0049", param, tables, saveResult);
		}
		
	
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Event Listeners
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 정상적으로 리턴된 결과를 처리
		 * 기타계좌 콤보박스 데이터 
		 * 
		 * @param
		 * @return void
		 */
		public function bankCodeResult(dr:DataReceiver):void
		{
			if(dr.retCode==0) {  
				bankCodeList = parseBankList(dr.result.BTAB);
				facade.sendNotification(WelfareFacade.LOAD_BANK_ACCOUNT_COMPLETE, this);
			}
			else {
				facade.sendNotification(WelfareFacade.LOAD_BANK_ACCOUNT_COMPLETE, this);
			}
		}
		/**
		 * 계좌관리 메인데이터 조회 성공
		 */
		public function accountResult(dr:DataReceiver):void
		{
			if(dr.retCode==0) {  
				parseBankAccount(dr.result.BTAB);
				facade.sendNotification(WelfareFacade.LOAD_BANK_ACCOUNT_COMPLETE, this);
			}
			else {
				facade.sendNotification(WelfareFacade.LOAD_BANK_ACCOUNT_COMPLETE, this);
			}
		}
		
		
		/**
		 * 기타계좌 수정 성공
		 */
		public function saveResult(dr:DataReceiver):void
		{
			if(dr.retCode==0) {  
				additionBank = tmpBank;
				facade.sendNotification(WelfareFacade.LOAD_ACCOUNT_SAVE_COMPLETE, this);
			}
			else {
				facade.sendNotification(WelfareFacade.LOAD_ACCOUNT_SAVE_FAILED, this);
			}
		}
		
		
		
		
		
		///////////////////////////////////////////////////////////////////////////////////////////////////
		//
		//	Data Parsing functions
		//
		///////////////////////////////////////////////////////////////////////////////////////////////////
		/**
		 * 서버에서 전달된 result 객체를 분석하여 Value Object를 생성
		 * 
		 * @param
		 * @return 선택적복리후생 초기화면 list
		 */
		private function parseBankList(srcArr:ArrayCollection):ArrayCollection
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:CodeVO = new CodeVO();
				vo.CODE = rec["BANKL"];
				vo.TEXT = rec["BANKA"];
				retArr.addItem(vo);
			} 
			return retArr;	
		}
		
		/****
		 * 계좌관리 메인데이터 조회
		 * ***/
		private function parseBankAccount(srcArr:ArrayCollection):void
		{
			var retArr:ArrayCollection = new ArrayCollection();
			
			for each(var rec:Object in srcArr) 
			{
				var vo:BankAccountVO = new BankAccountVO();
				vo.BNKSA = rec["BNKSA"];
				vo.STEXT = rec["STEXT"];
				vo.BANKL = rec["BANKL"];
				vo.BANKA = rec["BANKA"];
				vo.BANKN = rec["BANKN"];
				vo.EMFTX = rec["EMFTX"];
				vo.BANKS = rec["BANKS"];
				
				//vo.BNKTX = rec["BNKTX"];
				
				if(vo.BNKSA=="0") {
					this.defaultBank = vo;
				}
				else {
					this.additionBank = vo;
				}
			} 
		}
		
	}
}